package com.orange.common.util

import android.net.Uri
import com.blankj.utilcode.util.FileIOUtils
import com.blankj.utilcode.util.Utils
import java.io.File
import java.io.FileNotFoundException
import java.io.IOException
import java.io.InputStream

object UriUtils{
    /**
     * 将uri复制到缓存目录
     */
    fun copyUri2CacheFile(uri: Uri, fileName: String): File? {

        var inputStream: InputStream? = null

        try {
            inputStream = Utils.getApp().contentResolver.openInputStream(uri)
            val file = File(Utils.getApp().cacheDir, fileName)
            FileIOUtils.writeFileFromIS(file.absolutePath, inputStream)
            return file
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
            return null
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }
}